/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.smarttv.model;

import com.hp.hpl.jena.rdf.model.Resource;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import edu.yonsei.iwb.smarttv.querygraph.QueryCombination;
import java.util.Vector;

/**
 *
 * @author JDK
 */
public class ConceptualGraph extends Vector<ConceptArc> {
    private String cgID;
    
    public ConceptualGraph() {
        
    }

    public ConceptualGraph(QueryCombination qc) {
        for (int i = 0; i < qc.size() - 1; i++) {
            for (int j = i + 1; j < qc.size(); j++) {
                ResourceNode source = qc.get(i);
                ResourceNode target = qc.get(j);
                ConceptArc sub_cg;
                if(qc.get(i).getType() == NodeType.LITERAL_TYPE){
                    if (qc.get(j).getType() != NodeType.PROPERTY_TYPE && qc.get(j).getType() != NodeType.LITERAL_TYPE && qc.get(j).getType() != NodeType.INSTANCE_TYPE) {
                        
                        // LITERAL의 경우 Property 고려 안하고 있음 - 20120715
                        sub_cg = new ConceptArc(source, target, new WeightFactory(1), ArcType.LITERAL_TYPE);
                        this.add(sub_cg);
//                        break; // Literal value의 바로 옆에 있는 애만 아크로 생성
                    }
                }else if ((qc.get(i).getType() == NodeType.CLASS_TYPE || qc.get(i).getType() == NodeType.INSTANCE_TYPE) && (qc.get(j).getType() == NodeType.CLASS_TYPE || qc.get(j).getType() == NodeType.INSTANCE_TYPE)) {
                    if(qc.get(i).getType() == NodeType.INSTANCE_TYPE && qc.get(j).getType() == NodeType.INSTANCE_TYPE){
                        continue;
                    }else{
                        sub_cg = new ConceptArc(source, target, new WeightFactory(1));
                        for (int k = i + 1; k < j; k++) {
                            if (qc.get(k).getType() == NodeType.PROPERTY_TYPE) {
                                Resource property = qc.get(k).getNode();
                                sub_cg.setProperty(ResourceFactory.createProperty(property.getURI()));
                            }
                        }
                        this.add(sub_cg);
                    }
                }
            }
        }
    }
    
    public void updateConceptualTree(ConceptualGraph cg){
        for (int i = 0; i < cg.size(); i++) {
            this.add(cg.get(i));
        }
    }
        
    public void setCGID(String cgID){
        this.cgID = cgID;
    }
    public String getCGID(){
        return this.cgID;
    }

    @Override
    public String toString() {
        StringBuffer s = new StringBuffer();
        for (int i = 0; i < this.size(); i++) {
            ResourceNode head = this.get(i).getHead();
            ResourceNode tail = this.get(i).getTail();
            s.append(i + " 번째 Concept Arc의 Head Node = " + head.getNode() + ",  Tail Node = " + tail.getNode() + "\n");
            s.append(i + " 번째 Concept Arc의 Head instance = " + head.getInstance() + ",  Tail instance = " + tail.getInstance() + "\n");
            if (!this.get(i).getProperty().isEmpty()) {
                s.append(" 존재하는 Proeprty는 ");
                for (int j = 0; j < this.get(i).getProperty().size(); j++) {
                    s.append("[ " + this.get(i).getProperty().get(j) + " ]\n");
                }

            }
        }
        return s.toString();
    }
}
